package com.hm.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hm.ruiji.common.R;
import com.hm.ruiji.dto.SetmealDto;
import com.hm.ruiji.entity.Category;
import com.hm.ruiji.entity.Setmeal;
import com.hm.ruiji.entity.SetmealDish;
import com.hm.ruiji.service.CategoryService;
import com.hm.ruiji.service.SetMealDishService;
import com.hm.ruiji.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Resource
    private CategoryService categoryService;

    @Resource
    private SetMealService setMealService;

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {
        log.info("id = {}", id);
        SetmealDto setmealDto = setMealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }



    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setMealService.saveWithDish(setmealDto);
        return R.success("套餐添加成功");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> removeWithDish(@RequestParam List<Long> ids) {
        log.info("ids: {}",ids);
        boolean b = setMealService.removeWithDish(ids);
        if (!b) return R.error("你选中的套餐中包含在售产品，请停售后再删除");
        return R.success("删除成功！");
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setMealService.updateWithDish(setmealDto);
        return R.success("套餐信息修改成功");
    }

    /**
     * 启售停售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status, @RequestParam List<Long> ids) {
        UpdateWrapper<Setmeal> wrapper = new UpdateWrapper<>();
        wrapper.set("status", status).in("id", ids);
        setMealService.update(wrapper);
        return R.success("套餐状态修改成功");
    }


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {}" ,page,pageSize);

        // 创建分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPageInfo = new Page<>();

        // 创建查询构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        // 添加过滤条件
        queryWrapper.like(name != null, Setmeal::getName, name);
        //添加排序条件，根据sort排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        //执行查询
        setMealService.page(pageInfo,queryWrapper);

        /**
         * Params:
         * source – the source bean
         * target – the target bean
         * ignoreProperties – array of property names to ignore
         */
        // 对象拷贝
        BeanUtils.copyProperties(pageInfo, dtoPageInfo, "records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item, setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;

        }).collect(Collectors.toList());

        dtoPageInfo.setRecords(list);

        return R.success(dtoPageInfo);
    }

}
